<template>
  <el-dialog
    :title="form.id ? lang.t('common.update') : lang.t('common.create')"
    v-model="show"
    :width="500"
  >
    <el-form ref="formRef" :model="form" :rules="rules" label-position="top">
      <el-form-item :label="plugin.lang('pay.name')" prop="name">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item :label="plugin.lang('pay.norm')" prop="norm">
            <div style="width: 100%; overflow: hidden">
              <el-input-number v-model="form.norm" :min="0" :precision="2" style="width: 100%" controls-position="right" />
            </div>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="plugin.lang('pay.sort')" prop="sort">
            <div style="width: 100%; overflow: hidden">
              <el-input-number v-model="form.sort" :min="0" style="width: 100%" controls-position="right" />
            </div>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item :label="plugin.lang('pay.status')" prop="status">
            <div style="width: 100%; overflow: hidden">
              <el-select v-model="form.status" placeholder=" ">
                <el-option
                  v-for="(status, index) in plugin.langTm('pay.status_text')"
                  :key="index"
                  :label="status"
                  :value="index">
                </el-option>
              </el-select>
            </div>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="plugin.lang('pay.cashbox')" prop="cashbox">
            <div style="width: 100%; overflow: hidden">
              <el-select v-model="form.cashbox" placeholder=" ">
                <el-option
                  v-for="(cashbox, index) in plugin.langTm('pay.cashbox_text')"
                  :key="index"
                  :label="cashbox"
                  :value="index">
                </el-option>
              </el-select>
            </div>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" :loading="loading" @click="onUpdate">{{ form.id ? lang.t('common.update') : lang.t('common.create') }}</el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script>
import { ref } from 'vue'
import app from '@/app'
import payModel from '../../model/pay'

export default {
  setup() {
    // 对话框
    const show = ref(false)

    // 表单
    const formRef = ref(null)
    const form = ref({})

    // 验证
    const rules = {
      name: [
        { required: true, message: ' ', trigger: 'blur' }
      ],
      status: [
        { required: true, message: ' ', trigger: 'blur' }
      ],
      cashbox: [
        { required: true, message: ' ', trigger: 'blur' }
      ],
      sort: [
        { required: true, message: ' ', trigger: 'blur' }
      ],
    }

    // 打开
    const open = data => {
      form.value = Object.assign({}, data)
      show.value = true
    }

    // 更新
    const loading = ref(false)
    const onUpdate = () => {
      formRef.value.validate(valid => {
        if (valid) {
          loading.value = true
          payModel.update(form.value).then(() => {
            show.value = false
          }).finally(() => {
            loading.value = false
          })
        }
      })
    }

    return {
      lang: app.lang,
      plugin: app.plugin.index,
      show,
      open,
      formRef,
      form,
      rules,
      loading,
      onUpdate
    }
  }
}
</script>